/**
 * copyright geolabs 2007
 * author: Gérald Fenoy [gerald at geolabs.fr]
 *
 */

System.require("OctoXml");

/**
 * MultipleSelectAutoOptions Class
 */ 
MultipleSelectAutoOptions=SelectAutoOptions.extend();

MultipleSelectAutoOptions.define({
  _init: function(){
      this.callback=arguments[2];
      this.saved_callback=arguments[2];
      this.dependents=arguments[3];
      this.setName(arguments[0]);
      this.setInitialValues(arguments[1]);
      this.request=null;
    },

  newData: function(){
      var xmldoc = this.request.responseXML;
      this.request=null;

      document.getElementById("output").parentNode.style.display='none';
      document.getElementById("doc_log").style.display='none';
      document.getElementById("doc_viewer").style.display='none';

      var rnode = xmldoc.getElementsByTagName('needpart');
      var tt=document.getElementById('classe');
      if(rnode.length>0){
	tt.parentNode.style.display='block';
      }
      else
	tt.parentNode.style.display='none';

      var rnode = xmldoc.getElementsByTagName('novalue');
      if(rnode.length>0){
	for(var i=0;i<this.dependents.length;i++){
	  this.dependents[i].empty();
	  this.dependents[i].root.parentNode.style.display='none';
	}
	//document.getElementById("output").parentNode.style.display='block';
	if(!this.callback)
	  this.callback=this.saved_callback;
	this.callback();	  
	this.callback=null;
	this.empty();
	this.root.parentNode.style.display='none';
      }
      else{
	//document.getElementById("output").parentNode.style.display='none';
	this.callback=null;
	var haschange=null;
	if(xmldoc.getElementsByTagName(this.name).length>0 && xmldoc.getElementsByTagName(this.name)[0].getElementsByTagName('tuple').length>1){
	  var tmp=document.getElementById(this.name);
	  this.empty();
	  SelectOptions.fill(tmp,xmldoc.getElementsByTagName(this.name)[0].getElementsByTagName('tuple'));
	  tmp.parentNode.style.display='block';
	  tmp.onchange=null;
	  tmp.onchange=this.activate.bind(this);
	  haschange=true;
	}
	else{
	  this.empty();
	  this.root.parentNode.style.display='none';
	}
	for(var i=0;i<this.dependents.length;i++){
	    this.dependents[i].empty();
	    if(xmldoc.getElementsByTagName(this.dependents[i].name).length>0 && xmldoc.getElementsByTagName(this.dependents[i].name)[0].getElementsByTagName('tuple').length>0){

              if(this.dependents[i].hasDependents){
		//alert('dependent : '+this.dependents[i].dependent+" => "+this.dependents[i].dependentSelect);
		try{

		var toto=OctoXml.jsonFromElement(xmldoc.getElementsByTagName('result')[0])['result']['trimestret'];

		var tmpOut={};
		for(a in toto)
		  tmpOut[toto[a]['@id']]=((toto[a].tuple instanceof Array)?toto[a].tuple:Array(toto[a].tuple));

		//alert(OctoXml.dumpAsJson(tmpOut));
		this.dependents[i].setDependentValues(tmpOut);
		this.dependents[i].root.onchange=this.dependents[i].updateDependentValues.bind(this.dependents[i]);
		}catch(e){alert(e);}
	      }

	      SelectOptions.fill(this.dependents[i].root,xmldoc.getElementsByTagName(this.dependents[i].name)[0].getElementsByTagName('tuple'));
	      if(!haschange)
		this.dependents[i].root.parentNode.style.display='block';
	  }
	}
      }
      this.request=null;
    },
      
  activate: function(){
      var tmp=document.getElementById(this.name);
      var val=tmp.options[tmp.selectedIndex].value;
      //alert("VAL = "+val);
      for(var i=1;i<tmp.options.length;i++)
	if(val!=tmp.options[i].value){
	  var toto=document.getElementById(tmp.options[i].value);
	  toto.selectedIndex=0;
	  toto.parentNode.style.display="none";
	}
      var toDisplay=document.getElementById(val);
      if(toDisplay)
	toDisplay.parentNode.style.display='block';
    }
  });
